//*****************************************下拉框*****************************************
/*
id:''         组件编号，唯一，必须
value:'',     当前名称
config:''     配置参数，制定弹出菜单的组件、是否可以手动输入、弹出组件尺寸是否固定、为空时显示文字，例如：{list:target,isInput:true,isFixed:true,isDate:true,emptyText:""}
style:''      组件样式，可选
callback:''   回调函数
*/
function RxDropList() {
}

RxDropList.init = function(id, value, config, style, callback) {
    var ctrl = document.getElementById(id);
    if (!ctrl) {
        ctrl = document.createElement("div");
    }

    ctrl.dispose = function(isAll) {
        while (this.hasChildNodes()) {
            this.removeChild(this.lastChild);
        }
        if (this.config.list) {
            this.config.list.dispose(true);
        }
        if (isAll == true) {
            var parentNode = this.parentNode;
            if (parentNode) {
                parentNode.removeChild(this);
            }
        }
    };
    //ctrl.dispose();
	if (id) {
    	ctrl.id = id;
	}
    ctrl.rxtype = "RxDropList";
    if (config.isDate == true) {
        ctrl.className = "rxdroplist_date";
    } else {
        ctrl.className = "rxdroplist";
    }
    ctrl.value = value;
    ctrl.config = config;
    ctrl.callback = callback;
    ctrl.isMenu = true;

    if (style) {
        RxCommon.setStyle(ctrl, style);
    }

	ctrl.ce = RxCommon.ce;

    ctrl.panel = ctrl.ce(ctrl, (id ? ("_panel_" + id) : null), "div", "rxdroplist_panel");
    ctrl.panel.isMenu = true;
    ctrl.input = ctrl.ce(ctrl.panel, (id ? ("_input_"+id) : null), "input", "rxdroplist_input");
    ctrl.input.type = "text";
    ctrl.input.value = value;
    ctrl.input.isMenu = true;
    ctrl.tips = ctrl.ce(ctrl.panel, (id ? ("_tips_" + id) : null), "div", "rxdroplist_tip");
    ctrl.tips.innerHTML = RxCommon.toDefault(config.emptyText,"");
    ctrl.tips.isMenu = true;

    ctrl.setInput = function(isInput) {
        this.config.isInput = isInput;
        if (this.config.isInput == true) {
            this.input.removeAttribute("readonly");
        } else {
            this.input.setAttribute("readonly", true);
        }
    };
    ctrl.setInput(ctrl.config.isInput);

    if (ctrl.config.list) {
        ctrl.config.list._self = ctrl;
        ctrl.input.value = RxCommon.isEmpty(value) ? ctrl.config.list.getValue() : value;
    }

    ctrl.input.onfocus = function() {
        this._self.input.focus();
        this._self.tips.style.display = "none";
        if (this._self.config.isDate == true) {
            this._self.className = "rxdroplist_date_focus";
        } else {
            this._self.className = "rxdroplist_focus";
        }
    };
    ctrl.onclick = function() {
        this.input.focus();
        this.showMenu();
    };
    ctrl.input.onclick = function() {
        this._self.input.focus();
    };
    ctrl.tips.onclick = function() {
        this._self.input.focus();
    };
    ctrl.input.onblur = function() {
        this._self.refresh();
    };

	ctrl.input.onkeydown = function(e) {
		if (this._self.callback) {
			var rxevent = {};
			rxevent.e = e;
			rxevent.name = "onkeydown";
			rxevent.self = this._self._self;
			rxevent.target = this;
			rxevent.source = this._self;
			rxevent.value = this.value;
			this._self.callback(rxevent);
		}
		return true;
	};
	ctrl.input.onchange = function(e) {
		if (this._self.callback) {
			var rxevent = {};
			rxevent.name = "onchange";
			rxevent.self = this._self._self;
			rxevent.target = this;
			rxevent.source = this._self;
			rxevent.value = this.value;
			this._self.callback(rxevent);
		}
		return true;
	};

    ctrl.showMenu = function() {
        var menu = this.config.list;
        if (menu) {
            if ($(menu).css("display") == "block") {
                $(menu).css("display", "none");
            } else {
                $(menu).css("display", "block");
                var top = $(this).offset().top+$(this).height();
                var left = $(this).offset().left;
                if (top + $(menu).height() > document.documentElement.clientHeight) {
                    top = $(this).offset().top - $(menu).height();
                    top = top < 0 ? 0 : top;
                }
                if (left + $(menu).width() > document.documentElement.clientWidth) {
                    left = $(this).offset().left - ($(menu).width()-$(this).width());
                    left = left < 0 ? 0 : left;
                }
                $(menu).css("top", top + "px");
                $(menu).css("left", left + "px");
                if (this.config.isFixed !== true) {
                    $(menu).css("width", ($(this).width()+0) + "px");
                }
                if (menu.resize) {
                    menu.resize();
                }
            }
        }
        var menus = $(".rxtree,.rxtable,.rxmenu");
        for (var i = 0; i < menus.length; i++) {
            if (menus[i] != menu && menus[i].isMenu == true && $(menus[i]).css("display") == "block") {
                $(menus[i]).css("display", "none");
            }
        }
    };

    ctrl.setValue = function(value) {
        this.input.value = value;
        this.refresh();
    };

    ctrl.refresh = function() {
        if (RxCommon.isEmpty(this.input.value)) {
            this.tips.style.display = "block";
        } else {
            this.tips.style.display = "none";
        }
        if (this.config.isDate == true) {
            this.className = "rxdroplist_date";
        } else {
            this.className = "rxdroplist";
        }
    };

    ctrl.getValue = function() {
        return this.input.value;
    };

    ctrl.resize = function(width, height) {
        width = width ? width : $(this).width();
        height = height ? height : $(this).height();
        this.style.width = width + "px";
        this.style.height = height + "px";
        this.style.lineHeight = height + "px";
        this.panel.style.height = (height-2) + "px";
        this.panel.style.lineHeight = (height-2) + "px";
        this.input.style.height = (height-2) + "px";
        this.input.style.lineHeight = (height-2) + "px";
        this.tips.style.height = (height-2) + "px";
        this.tips.style.lineHeight = (height-2) + "px";
        this.style.backgroundPosition = "right " +((height-24)/2)+ "px";
    };

    ctrl.refresh();

    return ctrl;
};
